package org.journal.controller;

import org.journal.entity.UserEntity;
import org.journal.service.RegistrationService;
import org.journal.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class RegisterController {

	private static final String ERROR_USER_EXIST_MESSAGE = "User already existed. Please enter another username";
	private static final String ERROR_MESSAGE_DIFF_PASSWORD = "Passwords must be equal";
	private static final String LOGIN_JSP = "login";
	private static final String ERROR_ATTRIBUTE = "error";
	private static final String REQUEST_PARAM_USERNAME = "username";
	private static final String REQUEST_PARAM_PASSWORD = "password";
	private static final String REQUEST_PARAM_CONFIRM_PASSWROD = "confirmPassword";
	private static final String SUCCESS_REGISTRATION_REDIRECT_URL = "redirect:/journal";

	@Autowired
	private UserService userService;

	@Autowired
	private RegistrationService registrationService;

	@Autowired
	private UserDetailsService customUserDetailsService;
	
	@Autowired
	@Qualifier(value = "authenticationManager")
	private AuthenticationManager authMgr;

	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public String doRegister() {
		return LOGIN_JSP;
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public String registerUser(
			@RequestParam(REQUEST_PARAM_USERNAME) String username,
			@RequestParam(REQUEST_PARAM_PASSWORD) String password,
			@RequestParam(REQUEST_PARAM_CONFIRM_PASSWROD) String confirmPassword,
			ModelMap modelMap) {

		if (!confirmPassword.equals(password)) {
			modelMap.addAttribute(ERROR_ATTRIBUTE, ERROR_MESSAGE_DIFF_PASSWORD);
			return LOGIN_JSP;
		}

		UserEntity user = registrationService.checkUserExistence(username);

		if (user != null) {
			modelMap.addAttribute(ERROR_ATTRIBUTE, ERROR_USER_EXIST_MESSAGE);
			return LOGIN_JSP;
		}

		user = registrationService.createUser(username, password);

		UserDetails userDetails = customUserDetailsService
				.loadUserByUsername(user.getUsername());

		System.out.println(userDetails);
		UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
				userDetails, password, userDetails.getAuthorities());
		System.out.println("hoho " + authMgr);

		if (auth.isAuthenticated()) {
			SecurityContextHolder.getContext().setAuthentication(auth);
			System.out.println("tafa");
			return SUCCESS_REGISTRATION_REDIRECT_URL;
		}

		return LOGIN_JSP;
	}

}
